package com.qf.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.OrderService;
import com.qf.utils.DataSourceUtil;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            //事务处理
            DataSourceUtil.begin();//手动开启事务
            //1.向订单表中添加订单
            orderDao.add(order);
            //2.向订单详情表中添加数据
            for (Cart cart : cartList) {
                OrderDetail orderDetail = new OrderDetail(0, order.getId(), cart.getPid(), cart.getNum(), cart.getMoney());//通过遍历挨个创建每一个订单详情
                orderDao.addDetail(orderDetail);//将创建好的订单详情存入订单详情表
            }
            //3.清空购物车
            CartService cartService = new CartServiceImpl();
            cartService.clear(order.getUid());
            //4.事务提交
            DataSourceUtil.commit();
        } catch (Exception e) {
            try {
                //手动处理事务  事务回滚
                DataSourceUtil.rollback();//事务回滚
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        }finally {
            try {
                //手动处理事务  释放资源
                DataSourceUtil.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList = orderDao.select(uid);
        //通过order中的aid找到对应的address表中的id，然后获取地址信息，将地址信息存入order中
        //遍历订单列表
        if (orderList != null&&orderList.size()>0) {
            //获取地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addressList = addressService.find(uid);
            if (addressList != null) {
                for (Order order : orderList) {//遍历所有的订单
                    for (Address address : addressList) {//遍历到每一个订单，遍历一次订单中的所有地址
                        //订单表的aid字段与地址表的id匹配获取
                        if (order.getAid()==address.getId()){
                            order.setAddress(address);
                        }
                    }
                }
            }
        }
        return orderList;
    }

    //【查询单个订单】
    @Override
    public Order findByOid(String oid) {
        Order order = orderDao.selectByOid(oid);
        if (order != null) {
            //手动封装地址信息
            AddressService addressService = new AddressServiceImpl();
            List<Address> addressList = addressService.find(order.getUid());
            if (addressList != null) {
                for (Address address : addressList) {
                    if (address.getId()==order.getAid()){
                        order.setAddress(address);
                    }
                }
            }
        }
        return order;
    }

    //【查看订单详情】
    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails = orderDao.selectDetail(oid);
        if (orderDetails != null&&orderDetails.size()>=0) {
            //手动封装商品表(前端要输出商品信息)
            GoodsService goodsService = new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails) {
                Goods goods = goodsService.findById(orderDetail.getPid());//pid就是goods表的id
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }
}
